package com.soda.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.soda.bean.Student;
import com.soda.service.StudentService;
import com.soda.service.impl.StudentServiceImpl;
import com.soda.util.PageInfo;
import com.soda.util.SelectStudentCondition;

public class StudentServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		StudentService stus = new StudentServiceImpl();
		PrintWriter out = response.getWriter();
		Student s = new Student();
		HttpSession session = request.getSession();

		/**
		 * 
		 * 宗矿帅 登录判断是否存在此用户
		 */
		String method = request.getParameter("method");
		System.out.println(method);
		if (method != null) {
			

		

			
			/**
			 * 后台学生管理模糊查询
			 */
			
			if (method.equals("selectCon")) {
				
				SelectStudentCondition ssc = new SelectStudentCondition();
				String stuname = request.getParameter("stuname");
				if (stuname != null) {
						stuname = new String(stuname.getBytes("ISO-8859-1"),	"utf-8");
				}
				String stuid = request.getParameter("stuid");
				if (stuid != null) {
						stuid = new String(stuid.getBytes("ISO-8859-1"), "utf-8");
					if (stuname != null || stuname != "") {
						ssc.setStuname(stuname);
					}
					if (stuid != null || stuid != "") {
						ssc.setIdcard(stuid);
					}
					session.setAttribute("condi", ssc);
				} else if (session.getAttribute("condi") != null) {
					ssc = (SelectStudentCondition) session.getAttribute("condi");
				}	
				PageInfo pi = new PageInfo();
				int total = stus.queryConTotal(ssc);
				pi.setTotal(total);
				pi.setLastPage((total + pi.getPer() - 1) / pi.getPer());
				String page = request.getParameter("page");
			
				if (page != null&&page!="") {
					pi.setPage(Integer.parseInt(page));
				}
				List<Student> list = stus.selectStuByCondi(pi, ssc);
				
				session.setAttribute("stulist", list);
				session.setAttribute("page", pi);
				
				request.getRequestDispatcher("houtai/pages/stu/list.jsp")
						.forward(request, response);
				
				return;
			}


			
			
			if (method.equals("selectStuById")) {
				String stui = new String(request.getParameter("stuid")
						.getBytes("ISO-8859-1"), "utf-8");
				int stuid = Integer.parseInt(stui);
				Student stu = stus.selectStuById(stuid);
				session.setAttribute("stus", stu);
				response.sendRedirect("houtai/pages/stu/detail.jsp");
				return;
			}

			if (method.equals("selectEditStu")) {
				String stui = new String(request.getParameter("stuid")
						.getBytes("ISO-8859-1"), "utf-8");
				int stuid = Integer.parseInt(stui);
				Student stu = stus.selectStuById(stuid);
				session.setAttribute("stu", stu);
				response.sendRedirect("houtai/pages/stu/edit.jsp");
				return;
			}

			/**
			 * 后台ajax判断不允许修改用户名
			 */
			if (method.equals("ajaxUname")) {
				String uname = new String(request.getParameter("uname")
						.getBytes("ISO-8859-1"), "utf-8");
				Student stu = (Student) session.getAttribute("stu");
				if (uname.equals(stu.getStuLoginname())) {
					out.print(1);
				} else {
					out.print(0);
				}
			}

			if (method.equals("editStu")) {
				Student stu = (Student) session.getAttribute("stu");
				stu.setStuPassword(request.getParameter("pwd"));
				stu.setStuName(request.getParameter("rname"));
				stu.setStuSex(request.getParameter("gender").equals("男") ? 1: 0);
				stu.setStuIdcard(request.getParameter("uid"));
				stu.setStuPhone(request.getParameter("phone"));
				stu.setStuAddress(request.getParameter("address"));
				stu.setStuEmail(request.getParameter("email"));
				stu.setStuSchool(request.getParameter("school").trim() == "" ? ""
						: request.getParameter("school"));
				stu.setStuMajor(request.getParameter("major").trim() == "" ? ""
						: request.getParameter("major"));
				stu.setStuRemark(request.getParameter("remark").trim() == "" ? ""
						: request.getParameter("remark"));
				int stuid = stu.getStuId();
				int n = stus.updateHoutaiStu(stuid, stu);
				if (n == 1) {
					session.setAttribute("stus", stu);
					out.print("<script>alert('保存成功!');</script>");
					out.print("<script>location.href='houtai/pages/stu/detail.jsp';</script>");
					return;
				} else {
					out.print("<script>alert('保存失败，请重新修改');</script>");
					out.print("<script>location.href='houtai/pages/stu/edit.jsp';</script>");
					return;
				}
			}
			

		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
